#import "ToyWinGIF.h"
#import "gif.h"

@implementation ToyWinGIF

- (int)drawToyWin:(const char *)fileName Type:(int)type Num:(int)num
{
	FILE *fp;
	gifHeader *gh = NULL;
	commonInfo *cinf = NULL;
	int bits = 8, err = 0;
	unsigned char *map[5];

	if ((fp = fopen(fileName, "r")) == NULL)
		return Err_OPEN;
	if ((gh = loadGifHeader(fp, &err)) == NULL) {
		(void)fclose(fp);
		return err;
	}

	[self initLocateWindow:fileName
		Width:gh->width Height:gh->height Num:num];

/* Bitmap data of planes in 'map[]' is one block of memory area.
   map[0] is beginning of the area, and map[1] = map[0] + (size of plane),
   and so on. But, if the image is monochrome, map[1] is NULL.
   The area of map[0] and (commonInfo *)cinf are kept in an object of
   ToyView, and freed by it.
*/
	err = gifGetImage(fp, gh, &bits, map);
	cinf = gifInfo(gh, bits, (map[1] == NULL));
	freeGifHeader(gh);
	(void)fclose(fp);
	if (err) /* 警告のみ */
		errAlert(fileName, err);
	if ([self drawView:map info: cinf] == nil)
		return -1;

	return 0;
}

@end
